﻿using System;

namespace Common.Tools
{
    /// <summary>
    /// 单实例设计模式
    /// </summary>
    /// <typeparam name="T">需要实例化的类</typeparam>
    public class SingleTon<T> where T : class, new()
    {
        protected SingleTon() { }
        private static T _instance;
        private static readonly object _syncRoot = new object();
        /// <summary>
        /// 获取当前实例
        /// </summary>
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        //Double-Check双重检查锁定,对于多线程访问时控制  
                        if (_instance == null)
                        {
                            _instance = new T();
                        }
                    }
                }
                return _instance;
            }
        }
    }
}
